<?php
namespace App\Adshopuser\Models;
use Common\BaseModel;
class BShopOrder extends BaseModel
{
    public $res;
    public $expire;

    public function initialize()
    {
        parent::initialize();
        $this->setSource('b_shop_order');
    }

    public function getOrder($no)
    {
        $info = $this->findFirstByOrderNo($no);
        if (!$info) {
            return '订单不存在!';
        }
        $skuDb   = new BShopGoodsSku();
        $skuInfo = $skuDb->findFirstById($info->sku_id);
        if (!$skuInfo) {
            return 'sku_不存在!';
        }
        $goodsDb   = new BShopGoods();
        $goodsInfo = $goodsDb->findFirstById($info->goods_id);
        if (!$goodsInfo) {
            return '商品不存在!';
        }
        $valueDb                = new BShopSkuValue();
        $result['order_no']     = $info->order_no;
        $result['num']          = $info->num;
        $result['price']        = $info->price;
        $result['total_price']  = $info->total_price;
        $result['growth_value'] = $info->growth_value;
        $result['status']       = $info->order_status;
        $result['order_status'] = "";
        $result['goods_img']    = $skuInfo->imageUrl;
        $result['goods_name']   = $goodsInfo->name;
        $result['goods_id']     = $goodsInfo->id;
        $result['sku_value']    = $valueDb->getValueName($skuInfo->value_ids);
        return $result;
    }

    public function returnOrder($data)
    {
        $where = "user_id = {$data['user_id']} and order_no =  '{$data['order_no']}'";
        $info  = $this->findFirst($where);
        if ($info) {
            if (!in_array($info->order_status, [2, 4])) {
                return '此订单状态不能退货';
            }
            $info->order_status = 11;
            $info->updated_at   = date('Y-m-d H:i:s');
            if (!$info->save()) {
                return '订单状态修改失败!';
            }
            return true;
        } else {
            return '订单不存在';
        }
    }



    public function admin_get_list($params){
        $page = intval($params['page']);
        $size = intval($params['limit']);
        if ($page > 0) {
            $page = ($page - 1) * $size;
        }
        $condtions="g.shop_id={$params['shop_id']}";

        if(isset($params['keywords']) && !empty($params['keywords'])){
            $condtions .= " AND (u.phone='{$params['keywords']}' or u.nickname like '%{$params['keywords']}%') OR (o.order_sn like '%{$params['keywords']}%')";
        }
        if(isset($params['status']) && $params['status'] != ''){
            $condtions .= " AND o.status={$params['status']}";
        }

        $sql="
        SELECT o.order_sn,u.nickname,o.goods_img,u.phone,ad.name as ad_name,ad.phone as ad_phone,o.unit_price,o.end_price,o.num,o.status,o.express_id,e.express_name,e.express_sn,o.created_at,o.pay_type FROM `b_shop_order` as o
left join f_users as u on u.id=o.user_id
left join b_shop_goods as g on g.id=o.goods_id
left join b_shop_goods_sku as sku on sku.id=o.goods_sku_id
left join b_shop_order_address as ad on ad.id=o.address_id
left join b_shop_order_express as e on e.id=o.express_id
where $condtions
order by o.created_at desc
limit $page,$size";
        $items = $this->getDI()->get('dbMaster')->fetchAll($sql);
        foreach($items as $key=>$value){
            $items[$key]["phone"] =substr_replace($value['phone'], '****', 3, 4);
        }

        $sql_count="
        SELECT count(o.id) as count FROM `b_shop_order` as o
left join f_users as u on u.id=o.user_id
left join b_shop_goods as g on g.id=o.goods_id
left join b_shop_goods_sku as sku on sku.id=o.goods_sku_id
left join b_shop_address as ad on ad.id=o.address_id
left join b_shop_order_express as e on e.id=o.express_id
where $condtions limit 1";
        $g_count = $this->getDI()->get('dbMaster')->fetchOne($sql_count);
        $get_list['total'] = isset($g_count['count']) && !empty($g_count['count']) ? (int)$g_count['count'] : 0;
        $get_list['items'] = $items;
        return $get_list;
    }



    public function get_order_detail($order_sn){
        $condtions="o.order_sn='{$order_sn}'";
        $sql="
        SELECT o.id,o.order_sn,u.nickname,u.phone,u.id as user_id,ad.name as ad_name,ad.phone as ad_phone,ad.address,o.goods_img,o.unit_price,o.end_price as total_price,o.num,o.status,o.express_id,e.express_name,e.express_sn,e.express_logo,e.created_at as express_created_at,o.created_at,o.pay_type,o.pay_time,o.message,g.goods_sn,g.name as goods_name,g.detailed_img,g.original_price,sku.endprice,sku.value_ids,g.goods_sn FROM `b_shop_order` as o
left join f_users as u on u.id=o.user_id
left join b_shop_goods as g on g.id=o.goods_id
left join b_shop_goods_sku as sku on sku.id=o.goods_sku_id
left join b_shop_order_address as ad on ad.id=o.address_id
left join b_shop_order_express as e on e.id=o.express_id
where $condtions";
        $detail = $this->getDI()->get('dbMaster')->fetchOne($sql);
        if($detail){
            $sku_ids = str_replace('_',',',$detail['value_ids']);
            if(empty($sku_ids)){
                $sku_ids = "-1";
            }
            $BShopSkuValueDb = new BShopSkuValue();
            $value_name_arr = $BShopSkuValueDb->find(array(
                'conditions'=>"id in($sku_ids)",
                'columns'=>"value_name"
            ))->toarray();
            if($value_name_arr){
                $detail['sku_value_name'] = implode('-',array_column($value_name_arr,'value_name'));
            }else{
                $detail['sku_value_name'] = "";
            }
        }
        return $detail;
    }


    public function admin_get_order($params=[]){
        $condtions="created_at>='{$params['data_time']['start_at']}' AND created_at<='{$params['data_time']['end_at']}' AND shop_id={$params['shop_id']}";
        if(isset($params['order'])){
            $condtions .= " AND status={$params['status']}";
        }
        $result['count'] =$this->count(array(
            'conditions'=>$condtions,
        ));
        $all_price = $this->sum(array(
            'conditions'=>$condtions,
            'column'=>'end_price',
        ));
        $result['all_price'] = $all_price ? $all_price : '0';
        return $result;
    }
    

}
